import { PrismaService } from '@app/prisma';
import { ClassesEntity } from '@app/prisma/classes.entity/classes.entity';
import { TeacherEntity } from '@app/prisma/teacher.entity/teacher.entity';
import { TeacherOnClassesEntity } from '@app/prisma/teacher.on.classes.entity/teacher.on.classes.entity';
import { ForbiddenError } from '@nestjs/apollo';
import { UseGuards } from '@nestjs/common';
import { Args, Int, Mutation, Query, Resolver } from '@nestjs/graphql';
import { Prisma } from '@prisma/client';
import { GqlAuthPowerGuard, AccountPower } from 'src/auth/auth.guard';
import { TeacherService } from './teacher.service';

@Resolver()
export class TeacherResolver {
    constructor(
        private readonly prisma: PrismaService,
        private readonly teacherService: TeacherService
    ) { }

    @Mutation(() => [TeacherEntity])
    @UseGuards(new GqlAuthPowerGuard(AccountPower))
    async getTeacherByClassesIds(
        @Args("ids", { type: () => [Int] }) ids: number[]
    ) {
        return this.prisma.teacher.findMany({
            where: {
                TeacherOnClasses: {
                    some: {
                        classesId: {
                            in: ids
                        }
                    }
                }
            },
            include: {
                TeacherOnClasses: {
                    include: {
                        classes: true
                    }
                }
            }
        })
    }

    @Mutation(() => TeacherEntity)
    @UseGuards(new GqlAuthPowerGuard(AccountPower))
    addTeacher(
        @Args("name") name: string
    ) {
        return this.prisma.teacher.create({
            data: {
                name
            }
        })
    }

    @Query(() => [TeacherEntity])
    searchTeacher(
        @Args("name", { nullable: true }) name: string
    ) {
        const where: Prisma.TeacherWhereInput = {};
        if (!!name) {
            where.name = {
                contains: name
            }
        }
        return this.prisma.teacher.findMany({
            where,
            include: {
                _count: {
                    select: {
                        TeacherOnClasses: true
                    }
                }
            },
            orderBy: {
                id: "desc"
            }
        })
    }


    @Mutation(() => TeacherEntity)
    @UseGuards(new GqlAuthPowerGuard(AccountPower))
    async delTeacher(
        @Args("id", { type: () => Int }) id: number
    ) {
        const teacher = await this.prisma.teacher.findUnique({
            where: {
                id

            }
        })
        if (!teacher) throw new ForbiddenError("404")
        await this.prisma.teacherOnClasses.deleteMany({
            where: {
                teacherId: id
            }
        })
        await this.prisma.taskOnTeacherItem.deleteMany({
            where: {
                teacherId: id
            }
        })
        await this.prisma.userTaskItem.deleteMany({
            where: {
                userOnTaskTeacherId: id
            }
        })
        await this.prisma.userOnTask.deleteMany({
            where: {
                teacherId: id
            }
        })

        return await this.prisma.teacher.delete({ where: { id } })
    }

    @Mutation(() => [TeacherEntity])
    getAllTeacher() {
        return this.prisma.teacher.findMany({
            include: {
                TeacherOnClasses: true
            },
            orderBy: {
                id: "desc"
            }
        })
    }

    @Mutation(() => ClassesEntity)
    @UseGuards(new GqlAuthPowerGuard(AccountPower))
    saveClassesTeacher(
        @Args("ids", { type: () => [Int] }) ids: number[],
        @Args("classesId", { type: () => Int }) classesId: number
    ) {

        return this.prisma.classes.
            update({
                where: {
                    id: classesId
                },
                data: {
                    TeacherOnClasses: {
                        deleteMany: {
                            classesId
                        },
                        connectOrCreate: ids.map((teacherId) => ({
                            where: {
                                teacherId_classesId: {
                                    teacherId,
                                    classesId
                                }
                            },
                            create: {
                                teacherId
                            }
                        }))
                    }
                }
            })
    }
    @Mutation(() => TeacherOnClassesEntity)
    @UseGuards(new GqlAuthPowerGuard(AccountPower))
    async saveClassesTeacherByName(
        @Args("teacher") teacher: string,
        @Args("classes") classes: string
    ) {
        const teacherEntity = await this.teacherService.findOrCreateTeacher(teacher);
        const classesEntity = await this.teacherService.findOrCreateClasses(classes);
        return this.teacherService.findOrCreateClassesOnTeacher(classesEntity, teacherEntity)
    }

    @Mutation(() => TeacherOnClassesEntity)
    @UseGuards(new GqlAuthPowerGuard(AccountPower))
    delClassesTeacher(
        @Args("teacherId", { type: () => Int }) teacherId: number,
        @Args("classesId", { type: () => Int }) classesId: number
    ) {
        return this.prisma.teacherOnClasses.delete({
            where: {
                teacherId_classesId: {
                    teacherId,
                    classesId
                }
            }
        })
    }


}
